package day3;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ch1 {

    //73
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        Set<Integer> set_m = new HashSet<>();
        Set<Integer> set_n = new HashSet<>();


        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(matrix[i][j]==0){
                    set_m.add(i);
                    set_n.add(j);
                }
            }
        }
        set(matrix,set_m,set_n);
    }
    public void set(int[][] matrix,Set<Integer> set_m,Set<Integer> set_n){
        int m = matrix.length;
        int n = matrix[0].length;

        for(int i:set_m){
            for (int j = 0; j < n; j++) {
                matrix[i][j]=0;
            }
        }
        for (int i:set_n){
            for (int j = 0; j < m; j++) {
                matrix[j][i]=0;
            }
        }
    }

    //54
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();

        if(matrix[0].length==1){
            for (int i = 0; i < matrix.length; i++) {
                list.add(matrix[i][0]);
            }
            return list;
        }

        list.add(matrix[0][0]);
        matrix[0][0]=200;
        fun(matrix,list,0,0,0);

        return list;



    }

    public void fun(int[][] arr,List<Integer> list,int m,int n,int index){
        if((index%4)==0){
            if(n+1>=arr[0].length){
                return;
            }
            if(arr[m][n+1]>100){
                return;
            }
            int num = arr[0].length-1;
            for (int i = n+1; i < arr[0].length; i++) {
                if(arr[m][i]<=100){
                    list.add(arr[m][i]);
                    arr[m][i]=200;
                }else {
                    num = i-1;
                    break;
                }
            }
            fun(arr,list,m,num,index+1);
        } else if((index%4)==1){
            if(m+1>=arr.length){
                return;
            }
            if(arr[m+1][n]>100){
                return;
            }
            int num = arr.length-1;
            for (int i = m+1; i < arr.length; i++) {
                if(arr[i][n]<=100){
                    list.add(arr[i][n]);
                    arr[i][n]=200;
                }else{
                    num = i-1;
                    break;
                }
            }
            fun(arr,list,num,n,index+1);

        }else if((index%4)==2){
            if(n-1<0){
                return;
            }
            if(arr[m][n-1]>100){
                return;
            }
            int num = 0;
            for(int i=n-1;i>=0;i--){
                if(arr[m][i]<=100){
                    list.add(arr[m][i]);
                    arr[m][i]=200;
                }else {
                    num = i+1;
                    break;
                }
            }
            fun(arr,list,m,num,index+1);

        }else {
            if(m-1<0){
                return;
            }
            if(arr[m-1][n]>100){
                return;
            }
            int num = 0;
            for(int i=m-1;i>=0;i--){
                if(arr[i][n]<=100){
                    list.add(arr[i][n]);
                    arr[i][n]=200;
                }else {
                    num = i+1;
                    break;
                }
            }
            fun(arr,list,num,n,index+1);
        }
    }







    public static void main(String[] args) {



    }

}
